package com.comm.ui.main;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.comm.R;
import com.comm.module.bean.EventInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenxunlin01 on 2017/10/9.
 */

public class EventInfoAdapter extends BaseAdapter {

    private Activity activity;
    private List<EventInfo> data = new ArrayList<>();

    public EventInfoAdapter(Activity activity) {
        this.activity = activity;
    }

    public void setData(List<EventInfo> eventInfos) {
        if (data == null) {
            data = new ArrayList<EventInfo>();
        }
        data.clear();
        data.addAll(eventInfos);
        notifyDataSetChanged();
    }

    public void addData(List<EventInfo> eventInfos) {
        if (data == null) {
            data = new ArrayList<>();
        }
        data.addAll(eventInfos);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (data == null) {
            return 0;
        } else {
            return data.size();
        }
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(activity).inflate(R.layout.main_list_item_layout, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.title = (TextView) view.findViewById(R.id.event_title);
            view.setTag(viewHolder);
        }

        ViewHolder viewHolder = (ViewHolder) view.getTag();
        EventInfo eventInfo = (EventInfo) getItem(i);
        viewHolder.title.setText(eventInfo.title);

        return view;
    }

    class ViewHolder {
        public TextView title;
    }
}
